﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using NUnit.Framework; 

using Microsoft.Practices.Unity;

using ERPStore.Extensions;
using ERPStore.Tests;

namespace ERPStore.Checkout.Tests.Controllers
{
	[TestFixture]
	public class AnonymousCheckoutControllerTests : ERPStore.Tests.TestBase
	{
		public AnonymousCheckoutControllerTests()
		{

		}

		[SetUp]
		public void Setup()
		{
			base.Initialize();
		}

		[TearDown]
		public void TearDown()
		{
			base.TeardownHttpContext();
		}

		[Test]
		public void Shipping_With_Empty_Cart()
		{
			var m_Controller = CreateController<ERPStore.Checkout.Controllers.CheckoutController>();

			var result = m_Controller.Shipping() as System.Web.Mvc.RedirectToRouteResult;
			Assert.AreEqual(result.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.HOME);
		}

		[Test]
		public void AnonymousShipping_With_Cashcustomer()
		{
			var checkoutController = CreateController<ERPStore.Checkout.Controllers.CheckoutController>();
			var cartController = CreateController<ERPStore.Web.Cart.Controllers.CartController>();

			cartController.AddCartItem("XBOX360");

			var result = checkoutController.Shipping() as System.Web.Mvc.ViewResult;
			var cart = result.ViewData.Model as ERPStore.Models.OrderCart;

			Assert.AreEqual(result.ViewName, "shipping");

			var registrationService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.IAccountService>();
			var visitorId = ((ERPStore.Models.UserPrincipal)checkoutController.HttpContext.User).VisitorId;
			var registration = registrationService.GetRegistrationUser(visitorId);

			Assert.IsNotNull(registration);

			registration.Email = "test@email.com";
			registration.FaxNumber = "0102030405";
			registration.FirstName = "firstname";
			registration.LastName = "lastname";
			registration.PhoneNumber = "0102030405";
			registration.Password = "changemoi";

			registration.ShippingAddressCity = "ShpCity";
			registration.ShippingAddressCountryId = 250;
			registration.ShippingAddressRecipientName = "ShpRcptName";
			registration.ShippingAddressRegion = "ShpRegion";
			registration.ShippingAddressStreet = "ShpStreet";
			registration.ShippingAddressZipCode = "00000";
			registration.IsSameBillingAddress = true;

			var postShippingResult = checkoutController.Shipping(
				registration
				, registration.Email
				) as System.Web.Mvc.RedirectToRouteResult;

			Assert.AreEqual(postShippingResult.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.CHECKOUT_CONFIGURATION);

			var visitorId2 = ((ERPStore.Models.UserPrincipal)checkoutController.HttpContext.User).VisitorId;
			registration = registrationService.GetRegistrationUser(visitorId2);

			Assert.AreEqual(registration.FirstName, "firstname");

			var configurationResult = checkoutController.Configuration() as System.Web.Mvc.ViewResult;
			cart = configurationResult.ViewData.Model as ERPStore.Models.OrderCart;

			Assert.AreEqual(cart.BillingAddress.RecipientName, "ShpRcptName");
			Assert.AreEqual(cart.DeliveryAddress.RecipientName, "ShpRcptName");
			Assert.AreNotEqual(cart.GrandTaxTotal, 0);
			Assert.AreNotEqual(cart.GrandTotal, cart.GrandTotalWithTax);

			Assert.AreEqual(configurationResult.ViewName, string.Empty);

			var postConfigurationResult = checkoutController.Configuration(
				"message",
				"documentreference",
				false,
				0) as System.Web.Mvc.RedirectToRouteResult;

			Assert.AreEqual(postConfigurationResult.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.CHECKOUT_CONFIRMATION);

			var confirmationResult = checkoutController.Confirmation() as System.Web.Mvc.ViewResult;
			cart = confirmationResult.ViewData.Model as ERPStore.Models.OrderCart;

		}

		[Test]
		public void AnonymousShipping_With_Cashcustomer_Shipping_With_Export()
		{
			var checkoutController = CreateController<ERPStore.Checkout.Controllers.CheckoutController>();
			var cartController = CreateController<ERPStore.Web.Cart.Controllers.CartController>();
			var catalogService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ICatalogService>();

			var product = catalogService.GetProductByCode("XBOX360");

			cartController.AddCartItem(product.Code);

			var result = checkoutController.Shipping() as System.Web.Mvc.ViewResult;
			var cart = result.ViewData.Model as ERPStore.Models.OrderCart;

			Assert.AreEqual(result.ViewName, "shipping");

			var registrationService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.IAccountService>();
			var visitorId = ((ERPStore.Models.UserPrincipal)checkoutController.HttpContext.User).VisitorId;
			var registration = registrationService.GetRegistrationUser(visitorId);

			Assert.IsNotNull(registration);

			registration.Email = "test@email.com";
			registration.FaxNumber = "0102030405";
			registration.FirstName = "firstname";
			registration.LastName = "lastname";
			registration.PhoneNumber = "0102030405";
			registration.Password = "changemoi";

			registration.ShippingAddressCity = "ShpCity";
			registration.ShippingAddressCountryId = ERPStore.Models.Country.ESP.Id;
			registration.ShippingAddressRecipientName = "ShpRcptName";
			registration.ShippingAddressRegion = "ShpRegion";
			registration.ShippingAddressStreet = "ShpStreet";
			registration.ShippingAddressZipCode = "00000";
			registration.IsSameBillingAddress = true;

			var postShippingResult = checkoutController.Shipping(
				registration
				, registration.Email
				) as System.Web.Mvc.RedirectToRouteResult;

			Assert.AreEqual(postShippingResult.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.CHECKOUT_CONFIGURATION);

			var configurationResult = checkoutController.Configuration() as System.Web.Mvc.ViewResult;
			cart = configurationResult.ViewData.Model as ERPStore.Models.OrderCart;

			Assert.AreEqual(cart.GrandTaxTotal, 0);
			Assert.AreEqual(cart.GrandTotal, cart.GrandTotalWithTax);

			Assert.AreEqual(configurationResult.ViewName, string.Empty);
		}

		[Test]
		public void Shipping_With_Assigned_Cart()
		{
			var cartService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ICartService>();
			var catalogService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ICatalogService>();
			var offerService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.IOffersService>();

			var user = TestHelper.GetCustomerUserPrincipal();
			var visitorId = user.VisitorId;

			var cart = cartService.CreateCart(user);
			var product = catalogService.GetProductByCode("xbox360");

			offerService.ApplyOffers(product, user.CurrentUser);
			cartService.AddCartItem(cart, product, 1, "test", null, null);
			cartService.Save(cart);

			TeardownHttpContext();

			var cartController = CreateController<ERPStore.Web.Cart.Controllers.CartController>();
			var assignResult = cartController.Assign(cart.Code) as System.Web.Mvc.RedirectResult;
			var anonymousCheckoutController = CreateController<ERPStore.Checkout.Controllers.CheckoutController>();

			var shippingResult = anonymousCheckoutController.Shipping() as System.Web.Mvc.ViewResult;
			cart = shippingResult.Model as ERPStore.Models.OrderCart;

			Assert.AreEqual(shippingResult.ViewName, "shipping");
			Assert.IsNotNull(cart);
		}

	}
}
